#### REPLICATION FILE ANALYSES IN MAIN DOCUMENT ####
#### SETUP ####
## Packages
library(tidyverse) # workflow
library(haven) # Stata-like labels
library(DeclareDesign) # Tidyverse-friendly t-test
library(here) # relative path management

## Options
theme_set(theme_bw(base_size = 20)) # white background with grid lines

## Load data
load(here("credit.rda"))

## Recoding
# Create categories for treatment groups
credit = credit %>% mutate(naming = ifelse(name == 1, "Labeled", "Not Labeled"),
                           mayor = ifelse(male_mayor == 1, "Man mayor", "Woman mayor"),
                           bias = recode(particularism,
                                         "No" = "Unbiased",
                                         "Omit" = "No_info",
                                         "Yes" = "Biased"))

# arrange variables in logical order
credit$mayor = fct_relevel(credit$mayor, "Man mayor", "Woman mayor")

#### TABLE 1 ####
design_tab = credit %>% group_by(mayor, bias) %>% tally() %>% pivot_wider(names_from = "bias", values_from = "n")

colnames(design_tab) = c("Mayor Gender", "Biased", "No Info", "Unbiased")

design_tab

#### TABLE 2 ####
recall_margins = credit %>% 
  filter(!is.na(recall_gender)) %>% 
  group_by(mayor, recall_gender) %>% 
  tally()

recall_total = credit %>% 
  filter(!is.na(recall_gender)) %>%
  group_by(mayor) %>%
  tally()

recall = merge(recall_margins, recall_total, by = "mayor")

recall = recall %>% 
  mutate(recall_gender = as_factor(recall_gender)) %>% 
  mutate(recall_gender = recode(recall_gender,
                                "male" = "Man",
                                "female" = "Woman",
                                "no info" = "This information was not provided",
                                "dk" = "I don't know")) %>% 
  mutate(prop = n.x/n.y)

colnames(recall) = c("Mayor", "Recollection", "n", "N", "Proportion")

recall

#### FIGURE 1 ####
## Means by condition
means_vote = credit %>% 
  group_by(mayor, bias) %>% 
  summarize(avg = mean(vote_respond, na.rm = TRUE),
            conf.low = t.test(vote_respond)$conf.int[1],
            conf.high = t.test(vote_respond)$conf.int[2]) %>% 
  mutate(
    bias = ifelse(bias == "No_info", "No info", bias),
    outcome = "Would vote for mayor",
    mayor = ifelse(mayor == "Man mayor", "Man", "Woman")
  )

means_help = credit %>% 
  group_by(mayor, bias) %>% 
  summarize(avg = mean(help_reelect, na.rm = TRUE),
            conf.low = t.test(help_reelect)$conf.int[1],
            conf.high = t.test(help_reelect)$conf.int[2]) %>% 
  mutate(
    bias = ifelse(bias == "No_info", "No info", bias),
    outcome = "Program helps mayor win",
    mayor = ifelse(mayor == "Man mayor", "Man", "Woman")
  )

means = bind_rows(means_vote, means_help)

# reverse order of factor labels
means$outcome = fct_rev(means$outcome)

means$bias = fct_relevel(means$bias, "Unbiased", "No info", "Biased")

# Visualize
avg_plot = ggplot(means) +
  aes(x = mayor, y = avg, shape = bias, color = bias) +
  labs(subtitle = "Means by condition",
       x = "", 
       y = "Mean response") +
  scale_y_continuous(limits = c(1,4), breaks = 1:4,
                     labels = c("1\nNot at all\nlikely",
                                "2\nNot very\nlikely",
                                "3\nSomewhat\nlikely",
                                "4\nVery\nlikely")) +
  geom_linerange(aes(ymin = conf.low, ymax = conf.high), size = 1, position = position_dodge(width = 0.5)) +
  geom_point(size = 4, position = position_dodge(width = 0.5)) +
  geom_text(aes(x = mayor, y = conf.high + 0.3, label = bias), color = "black", size = 4,
            position = position_dodge(width = 0.5)) + 
  facet_wrap(~ outcome, ncol = 1) +
  coord_flip() +
  theme(legend.position = "none",
        panel.grid.major = element_blank(),
        plot.subtitle = element_text(hjust = 0.5),
        axis.title.x = element_text(size = 18)) +
  scale_color_viridis_d(begin = 0, end = 0.8)

avg_plot


## Difference in means
effs = rbind(
  ## vote_respond
  # Man mayor
  tidy(difference_in_means(vote_respond ~ biased, data = credit,
                           subset = mayor == "Man mayor" & bias != "Unbiased")) %>%
    mutate(mayor = "Man", comparison = "Biased vs. No info"),
  tidy(difference_in_means(vote_respond ~ unbiased, data = credit,
                           subset = mayor == "Man mayor" & bias != "Biased")) %>%
    mutate(mayor = "Man", comparison = "Unbiased vs. No info"),
  tidy(difference_in_means(vote_respond ~ unbiased, data = credit,
                           subset = mayor == "Man mayor" & bias != "No-info")) %>%
    mutate(mayor = "Man", comparison = "Unbiased vs. Biased"),
  # Woman mayor
  tidy(difference_in_means(vote_respond ~ biased, data = credit,
                           subset = mayor == "Woman mayor" & bias != "Unbiased")) %>%
    mutate(mayor = "Woman", comparison = "Biased vs. No info"),
  tidy(difference_in_means(vote_respond ~ unbiased, data = credit,
                           subset = mayor == "Woman mayor" & bias != "Biased")) %>%
    mutate(mayor = "Woman", comparison = "Unbiased vs. No info"),
  tidy(difference_in_means(vote_respond ~ unbiased, data = credit,
                           subset = mayor == "Woman mayor" & bias != "No-info")) %>%
    mutate(mayor = "Woman", comparison = "Unbiased vs. Biased"),
  ## help_reelect
  # Man mayor
  tidy(difference_in_means(help_reelect ~ biased, data = credit,
                           subset = mayor == "Man mayor" & bias != "Unbiased")) %>%
    mutate(mayor = "Man", comparison = "Biased vs. No info"),
  tidy(difference_in_means(help_reelect ~ unbiased, data = credit,
                           subset = mayor == "Man mayor" & bias != "Biased")) %>%
    mutate(mayor = "Man", comparison = "Unbiased vs. No info"),
  tidy(difference_in_means(help_reelect ~ unbiased, data = credit,
                           subset = mayor == "Man mayor" & bias != "No-info")) %>%
    mutate(mayor = "Man", comparison = "Unbiased vs. Biased"),
  # Woman mayor
  tidy(difference_in_means(help_reelect ~ biased, data = credit,
                           subset = mayor == "Woman mayor" & bias != "Unbiased")) %>%
    mutate(mayor = "Woman", comparison = "Biased vs. No info"),
  tidy(difference_in_means(help_reelect ~ unbiased, data = credit,
                           subset = mayor == "Woman mayor" & bias != "Biased")) %>%
    mutate(mayor = "Woman", comparison = "Unbiased vs. No info"),
  tidy(difference_in_means(help_reelect ~ unbiased, data = credit,
                           subset = mayor == "Woman mayor" & bias != "No-info")) %>%
    mutate(mayor = "Woman", comparison = "Unbiased vs. Biased")
) %>%  mutate(outcome = ifelse(outcome == "vote_respond",
                               "Would vote for mayor",
                               "Program helps mayor win"))

effs$outcome = fct_rev(effs$outcome)

effs$comparison = fct_relevel(effs$comparison,
                              "Unbiased vs. Biased",
                              "Unbiased vs. No info",
                              "Biased vs. No info")

# Visualize
eff_plot = ggplot(effs) +
  aes(x = mayor, y = estimate, color = comparison, shape = comparison) +
  labs(subtitle = "Differences in means",
       x = "", 
       y = "Estimates with 95% confidence intervals") +
  scale_y_continuous(limits = c(-0.4, 0.8), breaks = seq(-0.4, 0.8, by = 0.2)) +
  geom_hline(yintercept = 0, linetype = "dashed") +
  geom_point(size = 4, position = position_dodge(width = 0.5)) +
  geom_linerange(aes(ymin = conf.low, ymax = conf.high), size = 1, 
                 position = position_dodge(width = 0.5)) +
  geom_text(aes(x = mayor, y = conf.high + 0.22, label = comparison), color = "black", size = 4,
            position = position_dodge(width = 0.5)) +
  facet_wrap(~ outcome, ncol = 1) +
  coord_flip() +
  theme(legend.position = "none",
        panel.grid.major = element_blank(),
        plot.subtitle = element_text(hjust = 0.5),
        axis.title.x = element_text(size = 18)) +
  scale_color_viridis_d(begin = 0, end = 0.8)

eff_plot

#### FIGURE 2 ####
## See appendix .R for chi-squared tests

# subset to control only
control = credit %>% filter(bias == "No_info")

## left panel
a = ggplot(control) +
  aes(x = mayor_corrupt, y = ..prop.., fill = mayor) +
  geom_bar(position = position_dodge2()) + 
  scale_fill_viridis_d(begin = 0, end = 0.8) +
  ylim(0, 0.8) +
  scale_x_continuous(limits = c(0.5, 4.5), breaks = 1:4,
                     labels = c("1\nNot at all\nlikely",
                                "2\nNot very\nlikely",
                                "3\nSomewhat\nlikely",
                                "4\nVery\nlikely")) +
  labs(x = "Mayor was corrupt", y = "", fill = "") +
  theme(axis.text = element_text(size = 10))

## middle panel
b = ggplot(control) +
  aes(x = mayor_patron, y = ..prop.., fill = mayor) +
  geom_bar(position = position_dodge2()) + 
  scale_fill_viridis_d(begin = 0, end = 0.8) +
  ylim(0, 0.8) +
  scale_x_continuous(limits = c(0.5, 4.5), breaks = 1:4,
                     labels = c("1\nNot at all\nlikely",
                                "2\nNot very\nlikely",
                                "3\nSomewhat\nlikely",
                                "4\nVery\nlikely")) +
  labs(x = "Mayor offered patronage", y = "", fill = "") +
  theme(axis.text = element_text(size = 10))

## right panel
c = ggplot(control) +
  aes(x = mayor_buyvotes, y = ..prop.., fill = mayor) +
  geom_bar(position = position_dodge2()) + 
  scale_fill_viridis_d(begin = 0, end = 0.8) +
  ylim(0, 0.8) +
  scale_x_continuous(limits = c(0.5, 4.5), breaks = 1:4,
                     labels = c("1\nNot at all\nlikely",
                                "2\nNot very\nlikely",
                                "3\nSomewhat\nlikely",
                                "4\nVery\nlikely")) +
  labs(x = "Mayor buys votes", y = "", fill = "") +
  theme(axis.text = element_text(size = 10))

a
b
c

#### FIGURE 3 ####
## Means by condition
means_unbiased = credit %>% 
  group_by(mayor, bias) %>% 
  summarize(avg = mean(distrib_unbiased, na.rm = TRUE),
            conf.low = t.test(distrib_unbiased)$conf.int[1],
            conf.high = t.test(distrib_unbiased)$conf.int[2]) %>% 
  mutate(
    bias = ifelse(bias == "No_info", "No info", bias),
    outcome = "Box distributed to those most in need",
    mayor = ifelse(mayor == "Man mayor", "Man", "Woman")
  )

means_satisfy = credit %>% 
  group_by(mayor, bias) %>% 
  summarize(avg = mean(satisfy_respond, na.rm = TRUE),
            conf.low = t.test(satisfy_respond)$conf.int[1],
            conf.high = t.test(satisfy_respond)$conf.int[2]) %>% 
  mutate(
    bias = ifelse(bias == "No_info", "No info", bias),
    outcome = "R would be satisfied with program",
    mayor = ifelse(mayor == "Man mayor", "Man", "Woman")
  )

means2 = bind_rows(means_satisfy, means_unbiased)

# reorder factor labels
means2$outcome = fct_rev(means2$outcome)
means2$bias = fct_relevel(means2$bias, "Unbiased", "No info", "Biased")

# Visualize
avg_plot2 = ggplot(means2) +
  aes(x = mayor, y = avg, shape = bias, color = bias) +
  labs(subtitle = "Means by condition",
       x = "", 
       y = "Mean response") +
  scale_y_continuous(limits = c(1,4), breaks = 1:4,
                     labels = c("1\nNot at all\nlikely",
                                "2\nNot very\nlikely",
                                "3\nSomewhat\nlikely",
                                "4\nVery\nlikely")) +
  geom_linerange(aes(ymin = conf.low, ymax = conf.high), size = 1, position = position_dodge(width = 0.5)) +
  geom_point(size = 4, position = position_dodge(width = 0.5)) +
  geom_text(aes(x = mayor, y = conf.high+ 0.3, label = bias), color = "black", size = 4,
            position = position_dodge(width = 0.5)) + 
  facet_wrap(~ outcome, ncol = 1) +
  coord_flip() +
  theme(legend.position = "none",
        panel.grid.major = element_blank(),
        plot.subtitle = element_text(hjust = 0.5),
        axis.title.x = element_text(size = 18)) +
  scale_color_viridis_d(begin = 0, end = 0.8)

avg_plot2

## Differences in means
effs2 = rbind(
  ## distrib_unbiased
  # Man mayor
  tidy(difference_in_means(distrib_unbiased ~ biased, data = credit,
                           subset = mayor == "Man mayor" & bias != "Unbiased")) %>%
    mutate(mayor = "Man", comparison = "Biased vs. No info"),
  tidy(difference_in_means(distrib_unbiased ~ unbiased, data = credit,
                           subset = mayor == "Man mayor" & bias != "Biased")) %>%
    mutate(mayor = "Man", comparison = "Unbiased vs. No info"),
  tidy(difference_in_means(distrib_unbiased ~ unbiased, data = credit,
                           subset = mayor == "Man mayor" & bias != "No-info")) %>%
    mutate(mayor = "Man", comparison = "Unbiased vs. Biased"),
  # Woman mayor
  tidy(difference_in_means(distrib_unbiased ~ biased, data = credit,
                           subset = mayor == "Woman mayor" & bias != "Unbiased")) %>%
    mutate(mayor = "Woman", comparison = "Biased vs. No info"),
  tidy(difference_in_means(distrib_unbiased ~ unbiased, data = credit,
                           subset = mayor == "Woman mayor" & bias != "Biased")) %>%
    mutate(mayor = "Woman", comparison = "Unbiased vs. No info"),
  tidy(difference_in_means(distrib_unbiased ~ unbiased, data = credit,
                           subset = mayor == "Woman mayor" & bias != "No-info")) %>%
    mutate(mayor = "Woman", comparison = "Unbiased vs. Biased"),
  ## satisfy_respond
  # Man mayor
  tidy(difference_in_means(satisfy_respond ~ biased, data = credit,
                           subset = mayor == "Man mayor" & bias != "Unbiased")) %>%
    mutate(mayor = "Man", comparison = "Biased vs. No info"),
  tidy(difference_in_means(satisfy_respond ~ unbiased, data = credit,
                           subset = mayor == "Man mayor" & bias != "Biased")) %>%
    mutate(mayor = "Man", comparison = "Unbiased vs. No info"),
  tidy(difference_in_means(satisfy_respond ~ unbiased, data = credit,
                           subset = mayor == "Man mayor" & bias != "No-info")) %>%
    mutate(mayor = "Man", comparison = "Unbiased vs. Biased"),
  # Woman mayor
  tidy(difference_in_means(satisfy_respond ~ biased, data = credit,
                           subset = mayor == "Woman mayor" & bias != "Unbiased")) %>%
    mutate(mayor = "Woman", comparison = "Biased vs. No info"),
  tidy(difference_in_means(satisfy_respond ~ unbiased, data = credit,
                           subset = mayor == "Woman mayor" & bias != "Biased")) %>%
    mutate(mayor = "Woman", comparison = "Unbiased vs. No info"),
  tidy(difference_in_means(satisfy_respond ~ unbiased, data = credit,
                           subset = mayor == "Woman mayor" & bias != "No-info")) %>%
    mutate(mayor = "Woman", comparison = "Unbiased vs. Biased")
) %>%  mutate(outcome = ifelse(outcome == "distrib_unbiased",
                               "Box distributed to those most in need",
                               "R would be satisfied with program"))

effs2$outcome = fct_rev(effs2$outcome)

effs2$comparison = fct_relevel(effs2$comparison,
                               "Unbiased vs. Biased",
                               "Unbiased vs. No info",
                               "Biased vs. No info")

# Visualize
eff_plot2 = ggplot(effs2) +
  aes(x = mayor, y = estimate, color = comparison, shape = comparison) +
  labs(subtitle = "Differences in means",
       x = "", 
       y = "Estimates with 95% confidence intervals") +
  scale_y_continuous(limits = c(-0.4, 0.9), breaks = seq(-0.4, 0.8, by = 0.2)) +
  geom_hline(yintercept = 0, linetype = "dashed") +
  geom_point(size = 4, position = position_dodge(width = 0.5)) +
  geom_linerange(aes(ymin = conf.low, ymax = conf.high), size = 1, 
                 position = position_dodge(width = 0.5)) +
  geom_text(aes(x = mayor, y = conf.high + 0.25, label = comparison), color = "black", size = 4,
            position = position_dodge(width = 0.5)) +
  facet_wrap(~ outcome, ncol = 1) +
  coord_flip() +
  theme(legend.position = "none",
        panel.grid.major = element_blank(),
        plot.subtitle = element_text(hjust = 0.5),
        axis.title.x = element_text(size = 18)) +
  scale_color_viridis_d(begin = 0, end = 0.8)

eff_plot2
